Test Setup Failed
Pull Request — master (#1653)
by Aristeides
04:11 queued 02:02
created

kirkiSetSettingValue.setSelectWoo   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
/* jshint -W079 */
2
if ( _.isUndefined( window.kirkiSetSettingValue ) ) {
3
	var kirkiSetSettingValue = { // jscs:ignore requireVarDeclFirst
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
4
5
		/**
6
		 * Set the value of the control.
7
		 *
8
		 * @since 3.0.0
9
		 * @param string setting The setting-ID.
0 ignored issues
show
Documentation introduced by
The parameter string does not exist. Did you maybe forget to remove this comment?
Loading history...
10
		 * @param mixed  value   The value.
0 ignored issues
show
Documentation introduced by
The parameter mixed does not exist. Did you maybe forget to remove this comment?
Loading history...
11
		 */
12
		set: function( setting, value ) {
13
14
			/**
15
			 * Get the control of the sub-setting.
16
			 * This will be used to get properties we need from that control,
17
			 * and determine if we need to do any further work based on those.
18
			 */
19
			var $this = this,
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
20
			    subControl = wp.customize.settings.controls[ setting ],
21
			    valueJSON;
22
23
			// If the control doesn't exist then return.
24
			if ( _.isUndefined( subControl ) ) {
25
				return true;
26
			}
27
28
			// First set the value in the wp object. The control type doesn't matter here.
29
			$this.setValue( setting, value );
30
31
			// Process visually changing the value based on the control type.
32
			switch ( subControl.type ) {
33
34
				case 'kirki-background':
35
					if ( ! _.isUndefined( value['background-color'] ) ) {
36
						$this.setColorPicker( $this.findElement( setting, '.kirki-color-control' ), value['background-color'] );
37
					}
38
					$this.findElement( setting, '.placeholder, .thumbnail' ).removeClass().addClass( 'placeholder' ).html( 'No file selected' );
39
					_.each( ['background-repeat', 'background-position'], function( subVal ) {
40
						if ( ! _.isUndefined( value[ subVal ] ) ) {
41
							$this.setSelectWoo( $this.findElement( setting, '.' + subVal + ' select' ), value[ subVal ] );
42
						}
43
					});
44
					_.each( ['background-size', 'background-attachment'], function( subVal ) {
45
						jQuery( $this.findElement( setting, '.' + subVal + ' input[value="' + value + '"]' ) ).prop( 'checked', true );
46
					});
47
					valueJSON = JSON.stringify( value ).replace( /'/g, '&#39' );
48
					jQuery( $this.findElement( setting, '.background-hidden-value' ).attr( 'value', valueJSON ) ).trigger( 'change' );
49
					break;
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
50
51
				case 'kirki-code':
52
					jQuery( $this.findElement( setting, '.CodeMirror' ) )[0].CodeMirror.setValue( value );
53
					break;
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
54
55
				case 'checkbox':
56
				case 'kirki-switch':
57
				case 'kirki-toggle':
58
					value = ( 1 === value || '1' === value || true === value ) ? true : false;
0 ignored issues
show
Comprehensibility Best Practice introduced by
This re-assigns to the parameter value. Re-assigning to parameters often makes code less readable, consider introducing a new variable instead.
Loading history...
59
					jQuery( $this.findElement( setting, 'input' ) ).prop( 'checked', value );
60
					wp.customize.instance( setting ).set( value );
61
					break;
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
62
63
				case 'kirki-select':
64
				case 'kirki-preset':
65
				case 'kirki-fontawesome':
66
					$this.setSelectWoo( $this.findElement( setting, 'select' ), value );
67
					break;
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
68
69
				case 'kirki-slider':
70
					jQuery( $this.findElement( setting, 'input' ) ).prop( 'value', value );
71
					jQuery( $this.findElement( setting, '.kirki_range_value .value' ) ).html( value );
72
					break;
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
73
74
				case 'kirki-generic':
75
					if ( _.isUndefined( subControl.choices ) || _.isUndefined( subControl.choices.element ) ) {
76
						subControl.choices.element = 'input';
77
					}
78
					jQuery( $this.findElement( setting, subControl.choices.element ) ).prop( 'value', value );
79
					break;
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
80
81
				case 'kirki-color':
82
					$this.setColorPicker( $this.findElement( setting, '.kirki-color-control' ), value );
83
					break;
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
84
85
				case 'kirki-multicheck':
86
					$this.findElement( setting, 'input' ).each( function() {
87
						jQuery( this ).prop( 'checked', false );
88
					});
89
					_.each( value, function( subValue, i ) {
90
						jQuery( $this.findElement( setting, 'input[value="' + value[ i ] + '"]' ) ).prop( 'checked', true );
91
					});
92
					break;
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
93
94
				case 'kirki-multicolor':
95
					_.each( value, function( subVal, index ) {
96
						$this.setColorPicker( $this.findElement( setting, '.multicolor-index-' + index ), subVal );
97
					});
98
					break;
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
99
100
				case 'kirki-radio-buttonset':
101
				case 'kirki-radio-image':
102
				case 'kirki-radio':
103
				case 'kirki-dashicons':
104
				case 'kirki-color-palette':
105
				case 'kirki-palette':
106
					jQuery( $this.findElement( setting, 'input[value="' + value + '"]' ) ).prop( 'checked', true );
107
					break;
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
108
109
				case 'kirki-typography':
110
					_.each( ['font-family', 'variant', 'subsets'], function( subVal ) {
111
						if ( ! _.isUndefined( value[ subVal ] ) ) {
112
							$this.setSelectWoo( $this.findElement( setting, '.' + subVal + ' select' ), value[ subVal ] );
113
						}
114
					});
115
					_.each( ['font-size', 'line-height', 'letter-spacing', 'word-spacing'], function( subVal ) {
116
						if ( ! _.isUndefined( value[ subVal ] ) ) {
117
							jQuery( $this.findElement( setting, '.' + subVal + ' input' ) ).prop( 'value', value[ subVal ] );
118
						}
119
					});
120
121
					if ( ! _.isUndefined( value.color ) ) {
122
						$this.setColorPicker( $this.findElement( setting, '.kirki-color-control' ), value.color );
123
					}
124
					valueJSON = JSON.stringify( value ).replace( /'/g, '&#39' );
125
					jQuery( $this.findElement( setting, '.typography-hidden-value' ).attr( 'value', valueJSON ) ).trigger( 'change' );
126
					break;
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
127
128
				case 'kirki-dimensions':
129
					_.each( value, function( subValue, id ) {
130
						jQuery( $this.findElement( setting, '.' + id + ' input' ) ).prop( 'value', subValue );
131
					});
132
					break;
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
133
134
				case 'kirki-repeater':
135
136
					// Not yet implemented.
137
					break;
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
138
139
				case 'kirki-custom':
140
141
					// Do nothing.
142
					break;
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
143
				default:
144
					jQuery( $this.findElement( setting, 'input' ) ).prop( 'value', value );
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
145
			}
146
		},
147
148
		/**
149
		 * Set the value for colorpickers.
150
		 * CAUTION: This only sets the value visually, it does not change it in th wp object.
151
		 *
152
		 * @since 3.0.0
153
		 * @param object selector jQuery object for this element.
0 ignored issues
show
Documentation introduced by
The parameter object does not exist. Did you maybe forget to remove this comment?
Loading history...
154
		 * @param string value    The value we want to set.
0 ignored issues
show
Documentation introduced by
The parameter string does not exist. Did you maybe forget to remove this comment?
Loading history...
155
		 */
156
		setColorPicker: function( selector, value ) {
157
			selector.attr( 'data-default-color', value ).data( 'default-color', value ).wpColorPicker( 'color', value );
158
		},
159
160
		/**
161
		 * Sets the value in a selectWoo element.
162
		 * CAUTION: This only sets the value visually, it does not change it in th wp object.
163
		 *
164
		 * @since 3.0.0
165
		 * @param string selector The CSS identifier for this selectWoo.
0 ignored issues
show
Documentation introduced by
The parameter string does not exist. Did you maybe forget to remove this comment?
Loading history...
166
		 * @param string value    The value we want to set.
0 ignored issues
show
Documentation introduced by
The parameter string has already been documented on line 165. The second definition is ignored.
Loading history...
167
		 */
168
		setSelectWoo: function( selector, value ) {
169
			jQuery( selector ).selectWoo().val( value ).trigger( 'change' );
170
		},
171
172
		/**
173
		 * Sets the value in textarea elements.
174
		 * CAUTION: This only sets the value visually, it does not change it in th wp object.
175
		 *
176
		 * @since 3.0.0
177
		 * @param string selector The CSS identifier for this textarea.
0 ignored issues
show
Documentation introduced by
The parameter string does not exist. Did you maybe forget to remove this comment?
Loading history...
178
		 * @param string value    The value we want to set.
0 ignored issues
show
Documentation introduced by
The parameter string has already been documented on line 177. The second definition is ignored.
Loading history...
179
		 */
180
		setTextarea: function( selector, value ) {
181
			jQuery( selector ).prop( 'value', value );
182
		},
183
184
		/**
185
		 * Finds an element inside this control.
186
		 *
187
		 * @since 3.0.0
188
		 * @param string setting The setting ID.
0 ignored issues
show
Documentation introduced by
The parameter string does not exist. Did you maybe forget to remove this comment?
Loading history...
189
		 * @param string element The CSS identifier.
0 ignored issues
show
Documentation introduced by
The parameter string has already been documented on line 188. The second definition is ignored.
Loading history...
190
		 */
191
		findElement: function( setting, element ) {
192
			return wp.customize.control( setting ).container.find( element );
193
		},
194
195
		/**
196
		 * Updates the value in the wp.customize object.
197
		 *
198
		 * @since 3.0.0
199
		 * @param string setting The setting-ID.
0 ignored issues
show
Documentation introduced by
The parameter string does not exist. Did you maybe forget to remove this comment?
Loading history...
200
		 * @param mixed  value   The value.
0 ignored issues
show
Documentation introduced by
The parameter mixed does not exist. Did you maybe forget to remove this comment?
Loading history...
201
		 */
202
		setValue: function( setting, value, timeout ) {
203
			timeout = ( _.isUndefined( timeout ) ) ? 100 : parseInt( timeout, 10 );
0 ignored issues
show
Comprehensibility Best Practice introduced by
This re-assigns to the parameter timeout. Re-assigning to parameters often makes code less readable, consider introducing a new variable instead.
Loading history...
204
			wp.customize.instance( setting ).set({});
205
			setTimeout( function() {
206
				wp.customize.instance( setting ).set( value );
207
			}, timeout );
208
		}
209
	};
210
}
211